﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace 战地2服务器控制中心
{
    public partial class OP : Form
    {
        public OP()
        {
            InitializeComponent();
        }
        Socket FS;
        /// <summary>
        /// 连接按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TBconncet_Click(object sender, EventArgs e)
        {
            FS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse("192.168.1.6");
            IPEndPoint point = new IPEndPoint(ip, 4711);
            FS.Connect(point);
            Thread th = new Thread(FanHui);
            ShowLog("连接成功");
            th.IsBackground = true;
            th.Start();
        }
        /// <summary>
        /// 显示文本
        /// </summary>
        /// <param name="str"></param>
        public void ShowLog(string str)
        {
            TBxs.AppendText(str + "\r\n");
        }
        /// <summary>
        /// 返回的数据
        /// </summary>
        public void FanHui()
        {
            while (true)
            {
                byte[] buff = new byte[1024 * 1024 * 5];
                int r = FS.Receive(buff);
                string s = Encoding.UTF8.GetString(buff, 0, r);
                ShowLog(FS.RemoteEndPoint.ToString() + ":" + s);
            }
        }
        /// <summary>
        /// 发送按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BTfs_Click(object sender, EventArgs e)
        {
            byte[] buff = new byte[1024 * 1024 * 5];
            buff = Encoding.UTF8.GetBytes(TBfs.Text);
            FS.Send(buff);
            TBfs.Clear();
        }
        /// <summary>
        /// 窗口加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OP_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}
